// map.js
import qqMap from '../../../utils/qqMapsdk'
Page({
  data: {
    latitude: 40.060539,
    longitude: 116.343857,
    scale: 16,
    // 设置标记点，可以是多个数组
    markers: [
      {
        id: 1,
        latitude: 40.060539,
        longitude: 116.343857,
        width: 30,
        height: 40
      },
      {
        id: 2,
        latitude: 40.061539,
        longitude: 116.341847,
        iconPath: '../../../static/images/marker.png',
        width: 40,
        height: 40
      }
    ],
    repairDetail: {},
    // 路径规划
    polyline: []
  },
  onLoad({ id }) {
    // 获取报修详情的数据
    this.getRepairDetail(id)
    this.getPolyLine()
  },
  getPolyLine() {
    qqMap.direction({
      mode: 'bicycling',// 出行方式
      from: {latitude: 40.061539, longitude: 116.341847},
      to: { latitude: 40.060539, longitude: 116.343857},
      success:(res) => {
        console.log(res);
        // 读取压缩的路径坐标
        const coors = res.result.routes[0].polyline
        for (let i = 2; i < coors.length ; i++){
          coors[i] = coors[i-2] + coors[i]/1000000
        }
        console.log(coors);
        const points = []
        //将解压后的坐标放入点串数组pl中
        for (var i = 0; i < coors.length; i += 2) {
          points.push({ latitude: coors[i], longitude: coors[i + 1] })
        }
        console.log(points)
        this.setData({
          polyline: [
            {
              points,
              color: '#00d26a',
              width: 4,
            }
          ]
         
        })
      }
    })
  },
  // 报修详情接口
  async getRepairDetail(id) {
    if (!id) return wx.utils.toast('参数有误!')
    // 调用接口
    const { code, data: repairDetail } = await wx.http.get('/repair/' + id)
    // 检测接口是否调用成功
    if (code !== 10000) return wx.utils.toast()
    // 渲染数据
    this.setData({ repairDetail })
  },
})
